<?php 
namespace Home\Controller;
use Think\Controller;
use Home\Tool\CarTool;
class FlowController extends Controller{
	public function add(){
		$model = D('Goods');
		$goodsinfo = $model->find(I('get.goods_id'));
		if(!goodsinfo){
			$this->redirect('/');
			exit();
		}

		$car = CarTool::getIns();
		$car->add($goodsinfo['goods_id'],$goodsinfo['goods_neme'],$goodsinfo['shop_price']);
		$this->assign('che',$car->item());
		$this->diplay('Goods/checkOut');
	}

	public function done(){
		$car = CarTool::getIns();
		$oimodel = M('Ordinfo');
		$oimodel->create();
		$oimodel->ord_sn = $ord_sn = date('Ymd').rand(10000,99999);
		$oimodel->user_id = cookie('user_id')? cookie('user_id'): 0;
		$oimodel->money = $car->calcMoney();
		$oimodel->oretime = time();

		if($ordinfo_id = $oimodel->add()){
			$ogmodel = M('Ordgoods');
			$data = array();
			foreach($car->item() as $k=>$v){
				$row = array();
				$row['goods_id'] = $k;
				$row['goods_name'] = $v['goods_name'];
				$row['shop_price'] = $v['shop_price'];
				$row['goods_num'] = $v['num'];
				$row['ordinfo_id'] = $ordinfo_id;
				$data[] = $row;
			}

			if($ogmodle->addAll($data)){
				$this->assign('ord_sn',$ord_sn);
				$this->assign('money',$car->calcMoney());
				$this->display('Goods/done');
			}
		}
	}
}